package com.ruoyi.piliangruku.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.kucunpandian.service.IKucunpandianService;
import com.ruoyi.piliangruku.domain.PiliangRuku;
import com.ruoyi.piliangruku.service.IPiliangRukuService;
import com.ruoyi.shangpinxinxi.domain.Shangpinxinxi;
import com.ruoyi.shangpinxinxi.domain.TestResult;
import com.ruoyi.shangpinxinxi.service.IShangpinxinxiService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * 批量入库Controller
 * 
 * @author beilian
 * @date 2023-01-10
 */
@Controller
@RequestMapping("/piliangruku/rukuTest")
public class PiliangRukuController extends BaseController
{
    private String prefix = "piliangruku/rukuTest";

    @Autowired
    private IPiliangRukuService piliangRukuService;
    @Autowired
    private IKucunpandianService kucunpandianService;

    @Autowired
    private IShangpinxinxiService shangpinxinxiService;

    @RequiresPermissions("piliangruku:rukuTest:view")
    @GetMapping()
    public String rukuTest()
    {
        return prefix + "/rukuTest";
    }

    /**
     * 查询批量入库列表
     */
    @RequiresPermissions("piliangruku:rukuTest:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(HttpServletRequest request, PiliangRuku piliangRuku)
    {
            startPage();
            List<PiliangRuku> list = piliangRukuService.selectPiliangRukuList(piliangRuku);
            return getDataTable(list);
    }

    /**
     * 导出批量入库列表
     */
    @RequiresPermissions("piliangruku:rukuTest:export")
    @Log(title = "批量入库", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(HttpServletRequest request,PiliangRuku piliangRuku)
    {
            List<PiliangRuku> list = piliangRukuService.selectPiliangRukuList(piliangRuku);
            ExcelUtil<PiliangRuku> util = new ExcelUtil<PiliangRuku>(PiliangRuku.class);
            return util.exportExcel(list, "批量入库数据");
    }

    /**
     * 新增批量入库
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存批量入库
     */
    @RequiresPermissions("piliangruku:rukuTest:add")
    @Log(title = "批量入库", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(PiliangRuku piliangRuku)
    {
        double jiner=Double.parseDouble(piliangRuku.getDanJia())*Double.parseDouble(piliangRuku.getShuLiang());

        piliangRuku.setZongjine(BigDecimal.valueOf(jiner));

        gouhuoDanhaoSet(piliangRuku);

        TestResult testResult = new TestResult();
        testResult.setMenName(piliangRuku.getShangpinName());
        testResult.setMenKuan(piliangRuku.getGuiGeKuan());
        testResult.setMenGao(piliangRuku.getGuiGeGao());
        testResult.setKaiXiang(piliangRuku.getKaiXiang());
        if (piliangRuku.getShangpinLeibie().contains("防火锁")) {
            testResult.setMenName("%防火锁%");
            testResult.setMenKuan(null);
            testResult.setMenGao(null);
            testResult.setKaiXiang(null);
        }
        if (piliangRuku.getShangpinLeibie().contains("拉爆盖")) {
            testResult.setMenName("%拉爆盖%");
            testResult.setMenKuan(null);
            testResult.setMenGao(null);
            testResult.setKaiXiang(null);
        }
        if (piliangRuku.getShangpinLeibie().contains("闭门器")) {
            testResult.setMenName("%闭门器%");
            testResult.setMenKuan(null);
            testResult.setMenGao(null);
            testResult.setKaiXiang(null);
        }
        List<Shangpinxinxi> shangpinxinxi = shangpinxinxiService.selectShangpinxinxiByNameAndSize2(testResult);
        if (shangpinxinxi.size() == 0) {
            return toAjax("商品不存在");
        }
        if (piliangRuku.getDanJia() == null) {
            piliangRuku.setDanJia(shangpinxinxi.get(0).getShangpinShoujia().toString());
        }
        long zongjine = Long.parseLong(piliangRuku.getShuLiang())*Long.parseLong(piliangRuku.getShuLiang());
        piliangRuku.setZongjine(new BigDecimal(zongjine));
        piliangRuku.setTupian(shangpinxinxi.get(0).getTiaoma());

        return toAjax(piliangRukuService.insertPiliangRuku(piliangRuku));
    }

    /**
     * 修改批量入库
     */
    @RequiresPermissions("piliangruku:rukuTest:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        PiliangRuku piliangRuku = piliangRukuService.selectPiliangRukuById(id);
        mmap.put("piliangRuku", piliangRuku);
        return prefix + "/edit";
    }

    /**
     * 修改保存批量入库
     */
    @RequiresPermissions("piliangruku:rukuTest:edit")
    @Log(title = "批量入库", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(PiliangRuku piliangRuku)
    {
        if (piliangRuku.getDanJia() != null&&piliangRuku.getShuLiang()!=null) {
            double jiner=Double.parseDouble(piliangRuku.getDanJia())*Double.parseDouble(piliangRuku.getShuLiang());
            piliangRuku.setZongjine(BigDecimal.valueOf(jiner));
        }
        return toAjax(piliangRukuService.updatePiliangRuku(piliangRuku));
    }

    /**
     * 删除批量入库
     */
    @RequiresPermissions("piliangruku:rukuTest:remove")
    @Log(title = "批量入库", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(piliangRukuService.deletePiliangRukuByIds(ids));
    }

    private static long oldTime = 0;

    @PostMapping( "/inserts")
    @ResponseBody
    public AjaxResult inserts(HttpServletRequest request,String msg)
    {
        long newTime = System.currentTimeMillis();
        if (oldTime == 0 || newTime - oldTime < 2000) {
            oldTime = newTime;
            return toAjax("短时间多次重复录入");
        }
        oldTime = newTime;

        msg = msg.replace("\n","");

        String name4user = request.getSession().getAttribute("name4user").toString();

        PiliangRuku xiaoshouChuku = new PiliangRuku();

        gouhuoDanhaoSet(xiaoshouChuku);

        //msg = tiaoMaAuto.fromUnicode(msg);

        Shangpinxinxi shangpinxinxi = shangpinxinxiService.selectShangpinxinxiByDanhao(msg);
        if (shangpinxinxi != null) {
            msgSet(shangpinxinxi,xiaoshouChuku,name4user);
        }else {
            return toAjax("扫码录入失败");
        }
        return toAjax(piliangRukuService.insertPiliangRuku(xiaoshouChuku));
    }

    private void msgSet(Shangpinxinxi shangpinxinxi, PiliangRuku piliangRuku, String name) {
        //获取当前日期
        Date date = new Date(System.currentTimeMillis());
        //将时间格式化成yyyy-MM-dd HH:mm:ss的格式
//        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        String today = format.format(date);
        piliangRuku.setChukuDate(date);
        piliangRuku.setShangpinName(shangpinxinxi.getShangpinName());
        piliangRuku.setTupian(shangpinxinxi.getTiaoma());
        piliangRuku.setShangpinLeibie(shangpinxinxi.getShangpinLeibie());
        piliangRuku.setGuiGeKuan(shangpinxinxi.getGuiGeKuan());
        piliangRuku.setGuiGeGao(shangpinxinxi.getGuiGeGao());
        piliangRuku.setKaiXiang(shangpinxinxi.getKaiXiang());
        piliangRuku.setShuLiang("1");
        piliangRuku.setDanJia(shangpinxinxi.getShangpinShoujia().toString());
        //piliangRuku.setZongjine(shangpinxinxi.getShangpinShoujia());
        piliangRuku.setRukuMen(name);
    }

    private void gouhuoDanhaoSet(PiliangRuku xiaoshouChuku) {
        String numstr = piliangRukuService.selectGouhuoDanhaoByLastOne();
        int num;
        if (numstr == null || numstr.equals("")) {
            num = 1;
        }else {
            num = Integer.parseInt(numstr)+1;
        }

        String b = String.valueOf(num);
        if (b.length() < 8) {
            int a = 8-b.length();
            for (int j = 0; j < a; j++) {
                b="0" + b;
            }
        }
        xiaoshouChuku.setGouhuoDanhao(b);
    }
}
